//
//  MGTabPageScrollAdapter.m
//  Mango
//
//  Created by 胡超 on 2020/6/29.
//  Copyright © 2020 HuChao. All rights reserved.
//

#import "MGTabPageScrollAdapter.h"
#import "MGTabPageTableView.h"
#import "Macros.h"

@interface MGTabPageScrollAdapter()
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,assign) BOOL canScroll;
@property (nonatomic,assign) BOOL ignore;
@end

@implementation MGTabPageScrollAdapter

- (void)dealloc {
    DLog(@"MGTabPageScrollAdapter dealloc");
    [self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
}

- (instancetype)initWithScrollView:(UIScrollView *)scrollView {
    if (self = [super init]) {
        [self setScrollView:scrollView];
        [self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
        [NSNotificationCenter.defaultCenter addObserver:self
                                               selector:@selector(onNotificationChildCanScroll:)
                                                   name:@"MGChildCanScrollNotification"
                                                 object:nil];
        [NSNotificationCenter.defaultCenter addObserver:self
                                               selector:@selector(onNotificationShouldScrollToTop:)
                                                   name:@"MGShouldScrollToTopNotification"
                                                 object:nil];
    }
    return self;
}

#pragma mark - 处理联动

- (void)onNotificationChildCanScroll:(NSNotification *)notification {
    self.canScroll = [notification.object boolValue];
}

- (void)onNotificationShouldScrollToTop:(NSNotification *)notification {
    if ([self.scrollView isEqual:notification.object]) {
        return;
    }
    self.scrollView.contentOffset = CGPointMake(0, -self.scrollView.contentInset.top);
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    CGPoint newContentOffset = [change[NSKeyValueChangeNewKey] CGPointValue];
    CGPoint oldContentOffset = [change[NSKeyValueChangeOldKey] CGPointValue];
    if (CGPointEqualToPoint(newContentOffset, oldContentOffset)) {
        return;
    }
    if (self.scrollView.contentOffset.y+self.scrollView.contentInset.top == 0 && !self.scrollView.isDragging && !self.scrollView.isDecelerating) {
        return;
    }
    if (self.scrollView.contentOffset.y+self.scrollView.contentInset.top > 0) {
        [NSNotificationCenter.defaultCenter postNotificationName:@"MGParentCanScrollNotification" object:@(NO)];
    } else {
        [NSNotificationCenter.defaultCenter postNotificationName:@"MGParentCanScrollNotification" object:@(YES)];
        [NSNotificationCenter.defaultCenter postNotificationName:@"MGShouldScrollToTopNotification" object:self.scrollView];
    }
    
    if (!self.canScroll && !self.ignore) {
        self.ignore = YES;
        self.scrollView.contentOffset = CGPointMake(0, -self.scrollView.contentInset.top);
        self.ignore = NO;
    }
}

@end
